import os
from util import *
from CallGraph_clean_and_sample import *
from CallGraph_statistic_base import *
from CallGraph_request_number import *

def down_load_dataset(dataset_path):
    if os.path.exists(dataset_path):
        print(f"数据集已存在！（{dataset_path}）")
    else:
        print(f"开始下载数据集...")
        command=f"bash fetchData.sh start_date={start_date} end_date={end_date}"
        status=os.system(command)
        if status==0:
            print(f"数据集下载完毕。")
        else:
            print("下载出错！")
            exit(-1)


def get_time_str():
    from datetime import datetime

    # 获取当前时间
    now = datetime.now()

    # 格式化为字符串，例如：2025-05-29 14:30:00
    current_time_str = now.strftime("%m-%d-%H-%M-%S")
    return current_time_str




start_time=time.time()
parallel_num=5

# current_time_str=get_time_str()
# current_time_str="07-01-14-41-38"
current_time_str="common"
file_number=2
cur_dir=get_cur_dir()

#step 1: 下载数据集

start_date="0d11"
end_date="0d12"
dataset_dir_name=f"dataset_{start_date}_{end_date}"
dataset_path=cur_dir+"/"+dataset_dir_name
down_load_dataset(dataset_path)

#step 2:确定频率最高的前N个微服务名称(task >1) 初次筛选
high_frequency_N=100
out_file_name_call_num=f"CallGraph_{dataset_dir_name}_CallMStop{high_frequency_N}_{current_time_str}.csv"
out_file_path_topcall=cur_dir+"/dealed_data/"+out_file_name_call_num
statistic_service_request_number_multi_processing(dataset_path, out_file_path_topcall, high_frequency_N, file_number,parallel_num,S_or_MS="dm")

#step 3:清洗数据 （获取历史数据和验证数据）

seed=11
percent=0.005
out_file_name_clean_and_sample=f"CallGraph_{dataset_dir_name}_cleaned_withTOPMS_{percent}_s{seed}_history_{current_time_str}.csv"
out_file_path_hist=cur_dir+"/dealed_data/"+out_file_name_clean_and_sample
clean_and_sample_dataset_multi_processing(seed, percent, dataset_path,  out_file_path_hist, top_n_file_path=out_file_path_topcall, file_number=file_number,parallel_num=parallel_num)
seed=22
out_file_name_clean_and_sample=f"CallGraph_{dataset_dir_name}_cleaned_withTOPMS_{percent}_s{seed}_validate_{current_time_str}.csv"
out_file_path_vali=cur_dir+"/dealed_data/"+out_file_name_clean_and_sample
clean_and_sample_dataset_multi_processing(seed, percent, dataset_path,  out_file_path_vali, top_n_file_path=out_file_path_topcall, file_number=file_number,parallel_num=parallel_num)

#step 4:针对历史数据，统计每个类型MS被调用次数(目前没有用)
input_file_data_path=out_file_path_hist
out_file_path_ms_call_number=out_file_path_hist+".mscallnum.csv"
statistic_ms_call_number_for_specific_file(input_file_data_path,out_file_path_ms_call_number)

#step 5:将前面获取的call graph进行预处理   (需要时间比较长)
input_file_data_path=cur_dir+"/dealed_data/"+""
input_file_data_path=out_file_path_hist
statistic_and_get_base_request_multi_processing(input_file_data_path, top_n_file_path=None,parallel_num=10)

print(f"持续时间：{time.time()-start_time}")